# 回顾：xpath获取一个网页的title（层级关系）：/html/head/title
import requests
# from lxml import etree
from bs4 import BeautifulSoup
url = 'https://news.baidu.com/' # 以百度新闻网站为例
ua = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
req = requests.get(url, headers = ua)
# print("网页返回的内容：", req.content.decode('utf-8'))

html = req.content.decode('utf-8') # 一串字符串
# html = etree.HTML(html, parser=etree.HTMLParser(encoding='utf-8'))
# print("title: ", html.xpath('/html/head/title/text()'))

# 生成BeautifulSoup对象
soup = BeautifulSoup(html, 'lxml')
# print("输出格式化的BeautifulSoup对象：", soup.prettify()) # html/xml
# print("BeautifulSoup对象：", soup)
# print("BeautifulSoup对象的类型：", type(soup)) # 就是BeautifulSoup对象

# BeautifulSoup如何获取title标签
# print("获取title标签：", soup.title)
# print("获取head标签：", soup.head)

# 获取body标签中的第一个a标签
# print("获取第一个a标签：", soup.body.a)
# 获取所有的a标签
# print("所有a标签：", soup.find_all('a')) # 返回列表
# 所有名称为a标签的个数
# print("所有a标签的个数：", len(soup.find_all('a')))

# tag的name
tag = soup.a
# print("tag的name：", tag.name)
# print("tag的内容：", tag)
# 修改tag的name
# tag.name = 'b'
# print("修改name后tag的内容：", tag)

# # 获取tag对象的全部属性
# print("Tag对象的全部属性：", tag.attrs)
# # 获取href属性的值
# print("href属性的值：", tag['href'])
# # 新增属性id，赋值为wangye
# tag['id'] = 'wangye'
# print("增加了id属性以后的tag：", tag)
# # 删除data-path属性
# del tag['data-path']
# print("删除了data-path属性以后的tag：", tag)
# # 修改href属性的值
# tag['href'] = 'https://wandou.pro/'
# print("修改后tag的内容：",tag)

# 获取tag对象中包含的字符串（title）
tag = soup.title
print("Tag对象中包含的字符串：", tag.string)
print("tag.string的类型：", type(tag.string)) # 查看类型

# 替换字符串内容
print("替换前的tag：", tag)
tag.string.replace_with("豌豆PRO")
print("替换后的tag：", tag)